// /rule/addRuleInfo.do
document.write('<script type="text/javascript" src="../static/js/back/utils.js"></script>');
document.write('<script type="text/javascript" src="../static/js/back/ajax.js"></script>');
var _url_ = '../pic/addShopPic.do';
var _url_edit = '../pic/updateShopPic.do';
var _url_list = '../pic/getShopPicList';
var _url_id = '../pic/getShopPicById';
var _map = {};
///首页
var _pageNum = 1;
var _type ;
var _tid ;
var spid="";
  $(function(){
	   spid = getCookie("shop_id");
	  _type = getUrlParam("type");
	  _tid = getUrlParam("id");
	  if(!_tid){
		  console.log("typeId is null");
		  return;
	  }
	  _map["picTypeId"] = _tid;
	  console.log(_type);
	  console.log(_tid);
	  if(_type === "add"){
		  getTypeListAdd();
		  $("#shopId").val(spid);
	  }else if(_type === "edit"){
		  getTypeListAdd();
		  $(".is_up_do").hide();
		  showEditData();
	  }else if(_type == "choose"){
		  	$("#up-btn-zhu").hide();
		  	$(".layui-btn-danger").hide();
		  	$(".icon-download").parent().hide();
		  	$(".choosepic").show();
		  	getList();
	  }else{
		  //获取分类列表
//		  getTypeList();
//		  getTypeCount();
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  $(".search-btn").click(function(){
			 var tit =  $("#goodsTitle").val();
			 var stts =  $("#status").val();
			 console.log(tit);
			 _map["goodsTitle"] = tit;
			 _map["status"] = stts;
			 _pageNum = 1;
			 getList();
		  });
	  $("body").on("click",".left-type li",function(){
			 var tt = $(this).text();
			 $(this).addClass('lt-li-active').siblings().removeClass('lt-li-active');
			 _map['goodsTypeId'] = $(this).attr('data-status');
			 getList();
	  });
});
  function getList(){
	  _map["pageNum"] = _pageNum;
	  _map['shopId'] = spid;
	  _ajax_post_token(_url_list,_map,function(res){
		  console.log(res.returnObject);
		  if(!res.returnObject||res.returnObject.length<=0 ){
			  $(".layui-table tbody").html("");
			  $(".pic-list").html("");
			  $(".no-more-data").show();
			  return;
		  }else{
			  $(".no-more-data").hide();
		  }
		  var html = "";
		  if(!res.returnObject){
			  html = '<div class="layui-form-mid layui-word-aux no-more-data">暂无更多数据</div>';
			  $(".layui-table ").after(html);
			  return;
		  }
		  var stat = "" , statHtml = "";
		  showPageInfo(res.returnObject);
		  $(".x-right").text("共有数据："+res.returnObject.total +" 条");
		  $.each(res.returnObject.list,function(i,v,arr){
			  console.log(v);
			  html+='<li >'
			    	+'<div class="layui-unselect layui-form-checkbox ckec" lay-skin="primary" data-url="'+v.pic+'" data-id="'+v.id+'" ><i class="layui-icon">&#xe605;</i></div>'
			    	+'<div class="layui-anim" data-anim="layui-anim-scale"><img alt="" src="'+v.pic+'"></div>'   
			    	+'<div class="code">'+v.title+'</div>'
			    	+'<div class="pic-info ">'
			    	+'    <div><span>上传时间 : </span><span>'+formatDate(v.createTime,"YY-MM-DD")+'</span></div>'
			    	+'     <div><span>原图尺寸 : </span><span>'+v.picSize+'</span></div>'
			    	+' </div>'
			    	+' <div class=" pic-edit hide">'
			    	+'    <div><span onclick="member_del(this,\''+v.id+'\')">删除图片</span><span onclick="movePicType(\''+v.id+'\')">转移相册</span></div>'
			    	+'    <div class="copyDivText" data-clipboard-target="#ffo'+i+'"><span>复制图片路径</span></div>'
			    	+'</div>'
			    	+'<div class="copy-info">'
			    	+'     <input id="ffo'+i+'"  value="'+v.pic+'"/>'
			    	+' </div>'
			    	+'</li>';
			 
		  })
		  $(".pic-list").html(html);
	  })
  }
  function chooseOk(){
	  var data = tableCheck.getData("data-url");
	  console.log(data.join(","));
	  parent.returnChoose(data);
	  x_admin_close();
  }
  function movePicType(_id){
	  _ajax_post_token('../pic/getShopPicTypeList',{pageSize:100,shopId:spid},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = '<div class="x-body" style="height: auto;">'
			  		+'<style>.layui-form-select dl{height:auto;}</style>'
			  		 +'<form action="" method="post" class="layui-form" lay-filter="" onsubmit="javascript:return false;"><div class="layui-form-item">'
				      +'<label class="layui-form-label">选择相册</label>'
				      +' <div class="layui-input-inline">'
				      +'   <select class="pIdsel" name="goodsTypeId" lay-verify="" lay-search=""><option value="">请选择相册</option>';
		  $.each(list.list,function(i,v,arr){
			  console.log(v);
    			  html +='<option value="'+v.id+'">'+v.title+'</option>';
		  })
		  html +='  </select> </div> </div></form></div>';
		  layer.open({ title: '转移相册',
			  area:['451px','400px'],
			  content: html,
			  yes: function(index, layero){
				var typeId =   $(".pIdsel").val();
			    _ajax_post_token(_url_edit,{id:_id,picTypeId:typeId},function(re){
			    	if(re.resultCode = 1000){
			    		layer.msg('修改成功',{icon:6,time:2000});
			    		getList();
			    		parent.getList();
			    		layer.close(index); //如果设定了yes回调，需进行手工关闭
			    	}
			    });
			  }
		  });
		  form.render();
	  })
  }
  function moveAll(obj){
	  var data = tableCheck.getData();
	  var _id = data.join(",");
	  _ajax_post_token('../pic/getShopPicTypeList',{pageSize:100,shopId:spid},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = '<div class="x-body" style="height: auto;">'
			  +'<style>.layui-form-select dl{height:auto;}</style>'
			  +'<form action="" method="post" class="layui-form" lay-filter="" onsubmit="javascript:return false;"><div class="layui-form-item">'
			  +'<label class="layui-form-label">选择相册</label>'
			  +' <div class="layui-input-inline">'
			  +'   <select class="pIdsel" name="goodsTypeId" lay-verify="" lay-search=""><option value="">请选择相册</option>';
		  $.each(list.list,function(i,v,arr){
			  console.log(v);
			  html +='<option value="'+v.id+'">'+v.title+'</option>';
		  })
		  html +='  </select> </div> </div></form></div>';
		  layer.open({ title: '转移相册',
			  area:['451px','400px'],
			  content: html,
			  yes: function(index, layero){
				  var typeId =   $(".pIdsel").val();
				  _ajax_post_token(_url_edit,{moveids:_id,picTypeId:typeId},function(re){
					  if(re.resultCode = 1000){
						  layer.msg('修改成功',{icon:6,time:2000});
						  getList();
						  parent.getList();
						  layer.close(index); //如果设定了yes回调，需进行手工关闭
					  }
				  });
			  }
		  });
		  form.render();
	  })
  }
  function getTypeCount(){
	  
	  _ajax_post_token('../goods/selectGoodsCountByTypeId.do',{shopId:spid},function(res){
		  console.log(res);
		  if(!res.returnObject){
			  return;
		  }
		  $.each(res.returnObject,function(i,v,arr){
			  var li = $(".left-type ul li");
			  for (var k = 0; k < li.length; k++) {
				  var lis = li[k];
				if(  $(lis).attr("data-status") == v.typeId  ){
					console.log();
					$(lis).text($(lis).text()+"("+v.COUNT+")");
				}
			}
			  
		  })
	  })
  }
  
  
  function getTypeListAdd(){
	  console.log(spid);
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100,shopId:spid},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = "";
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".pIdsel").append(html);
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token(_url_id,{userType:1,id:_id},function(res){
		  console.log(res);
		 var ddt = res.returnObject;
		 ddt.priceOut = (ddt.priceOut/100).toFixed(2);
		 ddt.priceRoom = (ddt.priceRoom/100).toFixed(2);
		 ddt.goodsPackAmount = (ddt.goodsPackAmount/100).toFixed(2);
		  _showData = ddt;
	  });
  }    
      
  /*用户-删除*/
  function member_del(obj,_id){
      layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
    	  _url_ =_url_edit;
    	  _ajax_post_token(_url_,{status:0,id:_id},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      		getList();
    	      	  layer.msg('删除成功!',{icon:6,time:2000});
    	      	}else{
    	      		layer.msg('删除失败!',{icon:5,time:2000});
    	      	}
    	      });
      });
  }
 
  /*上下架*/
  function member_ud(obj,_id){
	  	  var stats = $(obj).attr('data-status');
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{status:stats,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('操作成功!',{icon: 6,time:1000});
			  }else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
  }
  //批量上架
  function member_udall(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:1,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 6,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  function member_udall2(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:2,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 1,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  
  
  /*用户-启动*/
  function member_start(obj,_id){
	  layer.confirm('确认要解封此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:1,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('已解封!',{icon: 6,time:1000});
			  }else if (res.resultCode == 1008){
				  layer.msg('当前账号已存在',{icon:5,time:2000});
			  }
			  else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
	  });
  }
      
  function updateInfo(data){
	  //发异步，把数据提交给后台
	 console.log(_url_);
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(res.resultCode == 1000){
      	  parent.getList();
      	  parent.layer.msg('操作成功!',{icon:1,time:2000});
      	  x_admin_close();
          // 获得frame索引
      	}else{
      		layer.msg('操作失败!',{icon:1,time:2000});
      	}
      });
  }
      
      
  function delAll (argument) {
      var data = tableCheck.getData();
      console.log(data.join(","));
      layer.confirm('确认要删除已选择的数据吗？',function(index){
          //捉到所有被选中的，发异步进行删除
    	  _ajax_post_token(_url_edit,{ids:data.join(",")},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      	  getList();
    	      	  layer.msg('操作成功!',{icon:1,time:2000});
    	          // 获得frame索引
    	      	}else{
    	      		layer.msg('操作失败!',{icon:1,time:2000});
    	      	}
    	      });
      });
    }      
       
      
function downloadCityExcel(){
	window.location.href = '../excel/cityDownExcel.do';
}   
      
      
//添加 和 修改窗口     